// pages/addressAdd/addressAdd.js
var QQMapWX=require('../../utils/qqmap-wx-jssdk.min.js')
var tcity = require("../../utils/citys.js");
var app=getApp()
Page({
  data:{
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false,
    number: true,
    addnumber: ''
  },
  chooseAddress: function(){
    this.setData({
      condition:true
    })
  },
  address:function(e){
    var url=app.data.url
   var formdata=e.detail.value
   if (e.detail.value.mobile.length == 0 || e.detail.value.contacts.length == 0 || e.detail.value.detail.length == 0) {
     var that = this;
     wx.showToast({
       title: `用户名和手机号不能为空！`,
       showCancel: false,
       duration: 2000
     })
   }else{
     wx.request({
       url: '' + url+'/Shop/UserCenter/SaveAddress',
       data: formdata,
       header: {
         'Content-Type': 'application/json'
       },
       success: function (res) {
         if (res.data.Status=='ok'){
           wx.navigateTo({
             url: '../address-list/address-list',
           })
         }
       },
       error: function (res) {
         console.log(res)
       }
     })
   }
  },
  bindChange: function (e) {
    console.log(e);
  },
  change: function (e) {
    var that = this;
    that.setData({
      number: false
    })
  },
  bindChange: function (e) {
    //console.log(e);
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;

    if (val[0] != t[0]) {
      console.log('province no ');
      const citys = [];
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name)
      }

      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })

      return;
    }
    if (val[1] != t[1]) {
      console.log('city no');
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
      }

      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0]
      })
      return;
    }
    if (val[2] != t[2]) {
      console.log('county no');
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }


  },
  open: function () {
    this.setData({
      condition: !this.data.condition
    })
  },
  onLoad:function(options){
    var id=options.id
    //收货地址
    var url = getApp().data.url
    var user = wx.getStorageSync('user').openid
    var that = this;
    that.setData({
      user: user,
      id: id
    })
    tcity.init(that);
    var cityData = that.data.cityData;
    const provinces = [];
    const citys = [];
    const countys = [];
    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i].name);
    }
    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name)
    }
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }
    that.setData({
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
      'province': cityData[0].name,
      'city': cityData[0].sub[0].name,
      'county': cityData[0].sub[0].sub[0].name
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})


